﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using java.io;
using java.util.zip;
using java.util;
using System.Collections.Generic;
using System.IO;

namespace EXAM
{
    public class MyZip
    {
        public List<ZipEntry> GetZipFiles(ZipFile zipfil)
        {
            List<ZipEntry> lstZip = new List<ZipEntry>();
            Enumeration zipEnum = zipfil.entries();
            while (zipEnum.hasMoreElements())
            {
                ZipEntry zip = (ZipEntry)zipEnum.nextElement();
                lstZip.Add(zip);
            }
            return lstZip;
        }

        public void Zip(string zipFileName, string[] sourceFile)
        {
            FileOutputStream filOpStrm = new FileOutputStream(zipFileName);
            ZipOutputStream zipOpStrm = new ZipOutputStream(filOpStrm);
            FileInputStream filIpStrm = null;

            foreach (string strFilName in sourceFile)
            {
                filIpStrm = new FileInputStream(strFilName);
                ZipEntry ze = new ZipEntry(Path.GetFileName(strFilName));
                zipOpStrm.putNextEntry(ze);
                sbyte[] buffer = new sbyte[1024];
                int len = 0;

                while ((len = filIpStrm.read(buffer)) >= 0)
                {
                    zipOpStrm.write(buffer, 0, len);
                }
            }

            zipOpStrm.closeEntry();
            filIpStrm.close();
            zipOpStrm.close();
            filOpStrm.close();
        }

        public string Extract(string zipFileName, string destinationPath)
        {
            string flag = "";
            FileInfo fInf = new FileInfo(zipFileName);
            //progressBar1.Visible = true;
            //progressBar2.Visible = true;

            //progressBar1.Minimum = 0;
            //progressBar1.Maximum = Convert.ToInt32((fInf.Length) / 100);

            //lstExtractedFiles.Items.Clear();
            ZipFile zipfile = new ZipFile(zipFileName);
            List<ZipEntry> zipFiles = GetZipFiles(zipfile);

            //progressBar2.Minimum = 0;
            //progressBar2.Maximum = zipFiles.Count;

            //lstExtractedFiles.Items.Add("Files Extracted to: " + destinationPath);
            //lstExtractedFiles.Items.Add("------------------------------------------------------------------------------------------------------------------------");
            foreach (ZipEntry zipFile in zipFiles)
            {
                if (!zipFile.isDirectory())
                {
                    InputStream s = zipfile.getInputStream(zipFile);
                    try
                    {
                        Directory.CreateDirectory(destinationPath + "\\" + Path.GetDirectoryName(zipFile.getName()));
                        FileOutputStream dest = new FileOutputStream(Path.Combine(destinationPath + "\\" + Path.GetDirectoryName(zipFile.getName()), Path.GetFileName(zipFile.getName())));
                        try
                        {
                            int len = 0;
                            sbyte[] buffer = new sbyte[8000];
                            while ((len = s.read(buffer)) >= 0)
                            {
                                dest.write(buffer, 0, len);
                                //if (progressBar1.Value + (8000 / 1000) < progressBar1.Maximum)
                                //{
                                //    progressBar1.Value = (progressBar1.Value + (8000 / 1000));
                                //}
                                //else
                                //{
                                //    string d = "";
                                //}
                                //progressBar1.Refresh();
                            }
                        }
                        catch (Exception ex){ flag = ex.Message; }
                        finally
                        {
                            dest.close();
                        }
                    }
                    catch (Exception ex) { flag = ex.Message; }
                    finally
                    {
                        s.close();
                    }
                }
                //lstExtractedFiles.Items.Add(zipFile.getName());
                //lstExtractedFiles.Refresh();
                //progressBar1.Value = 0;

                //progressBar2.Value = progressBar2.Value + 1;
                //progressBar2.Refresh();
            }
            //progressBar2.Value = 0;

            //progressBar1.Visible = false;
            //progressBar2.Visible = false;
            return flag;
        }
    }
}
